package com.sugree.jibjib;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

import com.sugree.jibjib.R;

public final class LocaleSettingActivity extends Activity {
	public static final String ACTION_LOCALE_ALERT = "com.sugree.jibjib.action.LOCALE_ALERT";

	public static final String KEY_FETCH_FRIENDS_FREQUENCY_ID = "fetch_friends_frequency_id";
	public static final String KEY_FETCH_FRIENDS_FREQUENCY_VALUE = "fetch_friends_frequency_value";
	public static final String KEY_FETCH_MENTIONS_FREQUENCY_ID = "fetch_mentions_frequency_id";
	public static final String KEY_FETCH_MENTIONS_FREQUENCY_VALUE = "fetch_mentions_frequency_value";
	public static final String KEY_FETCH_MESSAGES_FREQUENCY_ID = "fetch_messages_frequency_id";
	public static final String KEY_FETCH_MESSAGES_FREQUENCY_VALUE = "fetch_messages_frequency_value";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.locale_setting);

		setupFrequency(R.id.locale_friends_frequency, KEY_FETCH_FRIENDS_FREQUENCY_ID);
		setupFrequency(R.id.locale_mentions_frequency, KEY_FETCH_MENTIONS_FREQUENCY_ID);
		setupFrequency(R.id.locale_messages_frequency, KEY_FETCH_MESSAGES_FREQUENCY_ID);
	}

	protected void setupFrequency(int resource, String key) {
		final Spinner frequency = (Spinner)findViewById(resource);
		frequency.setSelection(getIntent().getIntExtra(key, 0));
		frequency.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				updateResult();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	}

	private void updateResult() {
		final Intent intent = new Intent();
		String blurb = "";
		intent.putExtra(com.twofortyfouram.Intent.EXTRA_STRING_ACTION_FIRE, ACTION_LOCALE_ALERT);
		blurb = fillFrequencyIntent(
				intent,
				R.id.locale_friends_frequency,
				KEY_FETCH_FRIENDS_FREQUENCY_ID,
				KEY_FETCH_FRIENDS_FREQUENCY_VALUE) + " / ";
		blurb += fillFrequencyIntent(
				intent,
				R.id.locale_mentions_frequency,
				KEY_FETCH_MENTIONS_FREQUENCY_ID,
				KEY_FETCH_MENTIONS_FREQUENCY_VALUE) + " / ";
		blurb += fillFrequencyIntent(
				intent,
				R.id.locale_messages_frequency,
				KEY_FETCH_MESSAGES_FREQUENCY_ID,
				KEY_FETCH_MESSAGES_FREQUENCY_VALUE);
		intent.putExtra(com.twofortyfouram.Intent.EXTRA_STRING_BLURB, blurb);
		setResult(RESULT_OK, intent);
	}

	protected String fillFrequencyIntent(Intent intent, int resource, String keyId, String keyValue) {
		final Spinner spinner = (Spinner)findViewById(resource);
		final int i = spinner.getSelectedItemPosition();
		final String v = (String)spinner.getSelectedItem();

		intent.putExtra(keyId, i);
		intent.putExtra(keyValue, v);
		return v;
	}

	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		super.onCreateOptionsMenu(menu);

		getMenuInflater().inflate(R.menu.locale_setting, menu);

		menu.findItem(R.id.menu_save).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
			public boolean onMenuItemClick(final MenuItem item) {
				updateResult();
				finish();
				return true;
			}
		});

		menu.findItem(R.id.menu_dontsave).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
			public boolean onMenuItemClick(final MenuItem item) {
				setResult(RESULT_CANCELED);
				finish();
				return true;
			}
		});
		return true;
	}
}
